package com.hxzy.controller.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/10/13 15:45
 * @Version 1.0
 */
@Component
public class SentinelFlowRuleConfig {

    @PostConstruct
    public void initRule(){
       // initFlowRule();
    }

    /**
     * 初始化流控规则
     */
    public void initFlowRule(){
        List<FlowRule>  arr=new ArrayList<>();

        ///mail/order/page  限流

        FlowRule  mailOrderPageRule=new FlowRule();
        mailOrderPageRule.setResource("/mail/order/page");
        mailOrderPageRule.setCount(20);
        //1 QPS      0 并发线程数
        mailOrderPageRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        mailOrderPageRule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);

        arr.add(mailOrderPageRule);
        FlowRuleManager.loadRules(arr);
    }

}
